class Machine < ActiveRecord::Base
  has_many :events
  has_many :states

  accepts_nested_attributes_for :states
  accepts_nested_attributes_for :events
  def initial_state
    @initial_state||=(states.first ? states.first.name : nil)
  end

  def states_hash
    @states_hash||=states.collect{|state|{state.name=>state}}
  end
  
  def events_hash
    @events_hash||=events.collect{|event|{event.name=>event}}
  end

  def state(name,options={})
    states_hash[name]
  end
  def event(name,options={})
    events_hash[name]
  end

end
